//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "FailureToPayEvent.h"
using namespace Cephei::QL::Experimental::Credit;
#include <gen/QL/Currency.h>
#include <gen/QL/Experimental/Credit/DefaultType.h>
#include <gen/QL/Experimental/Credit/DefaultProbKey.h>
#include <gen/QL/Experimental/Credit/DefaultEvent.h>
using namespace Cephei::QL;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Experimental::Credit::CFailureToPayEvent::CFailureToPayEvent (DateTime creditEventDate, Cephei::QL::ICurrency^ curr, QL::Experimental::Credit::SeniorityEnum bondsSen, Double defaultedAmount, DateTime settleDate, Double recoveryRates) : CDefaultEvent(CFailureToPayEvent::typeid)
{
    CCurrency^ _Ccurr;
    try
    {
#ifdef HANDLE
        _phFailureToPayEvent = NULL;
#endif
        QuantLib::Date _creditEventDate = (QuantLib::Date)ValueHelper::Convert (creditEventDate);
        _Ccurr = safe_cast<CCurrency^> (curr);
        _Ccurr->Lock();
        QuantLib::Currency& _curr = static_cast<QuantLib::Currency&> (_Ccurr->GetReference ()); 
        QuantLib::Seniority _bondsSen = (QuantLib::Seniority)bondsSen ;
        QuantLib::Real _defaultedAmount = (QuantLib::Real)ValueHelper::Convert (defaultedAmount);
        QuantLib::Date _settleDate = (QuantLib::Date)ValueHelper::Convert (settleDate);
        QuantLib::Real _recoveryRates = (QuantLib::Real)ValueHelper::Convert (recoveryRates);
        _ppFailureToPayEvent = new boost::shared_ptr<QuantLib::FailureToPayEvent> (new QuantLib::FailureToPayEvent ( _creditEventDate,  _curr,  _bondsSen,  _defaultedAmount,  _settleDate,  _recoveryRates ));
        SetDefaultEvent (boost::dynamic_pointer_cast<QuantLib::DefaultEvent> (*_ppFailureToPayEvent));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Ccurr != nullptr) _Ccurr->Unlock();
    }
}
Cephei::QL::Experimental::Credit::CFailureToPayEvent::CFailureToPayEvent (boost::shared_ptr<QuantLib::FailureToPayEvent>& childNative, Object^ owner) : CDefaultEvent(CFailureToPayEvent::typeid)
{
#ifdef HANDLE
	_phFailureToPayEvent = NULL;
#endif
	_ppFailureToPayEvent = &childNative;
    _ppDefaultEvent = new boost::shared_ptr<QuantLib::DefaultEvent> (boost::dynamic_pointer_cast<QuantLib::DefaultEvent> (*_ppFailureToPayEvent));
}
Cephei::QL::Experimental::Credit::CFailureToPayEvent::CFailureToPayEvent (QuantLib::FailureToPayEvent& childNative, Object^ owner) : CDefaultEvent(CFailureToPayEvent::typeid)
{
#ifdef HANDLE
	_phFailureToPayEvent = NULL;
#endif
	_ppFailureToPayEvent = new boost::shared_ptr<QuantLib::FailureToPayEvent> (&childNative);
    _ppDefaultEvent = new boost::shared_ptr<QuantLib::DefaultEvent> (boost::dynamic_pointer_cast<QuantLib::DefaultEvent> (*_ppFailureToPayEvent));
    _FailureToPayEventOwner = owner;
    _DefaultEventOwner = owner;
}

Cephei::QL::Experimental::Credit::CFailureToPayEvent::CFailureToPayEvent (CFailureToPayEvent^ copy) : CDefaultEvent(CFailureToPayEvent::typeid)
{
#ifdef HANDLE
	_phFailureToPayEvent = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppFailureToPayEvent = new boost::shared_ptr<QuantLib::FailureToPayEvent> (copy->GetShared());
        _ppDefaultEvent = new boost::shared_ptr<QuantLib::DefaultEvent> (boost::dynamic_pointer_cast<QuantLib::DefaultEvent> (*_ppFailureToPayEvent));
    }
}
Cephei::QL::Experimental::Credit::CFailureToPayEvent::CFailureToPayEvent (System::Type^ t) : CDefaultEvent(CFailureToPayEvent::typeid)
{
#ifdef HANDLE
	_phFailureToPayEvent = NULL;
#endif
	if (!t->IsSubclassOf(CFailureToPayEvent::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Experimental::Credit::CFailureToPayEvent::CFailureToPayEvent (QuantLib::Handle<QuantLib::FailureToPayEvent>& childNative, Object^ owner)  : CDefaultEvent(CFailureToPayEvent::typeid)
{
	_phFailureToPayEvent = &childNative;
	_ppFailureToPayEvent = &static_cast<boost::shared_ptr<QuantLib::FailureToPayEvent>>(childNative.currentLink());
    _ppDefaultEvent = new boost::shared_ptr<QuantLib::DefaultEvent> (boost::dynamic_pointer_cast<QuantLib::DefaultEvent> (*_ppFailureToPayEvent));
    _FailureToPayEventOwner = owner;
}
Cephei::QL::Experimental::Credit::CFailureToPayEvent::CFailureToPayEvent (QuantLib::Handle<QuantLib::FailureToPayEvent> childNative)  : CDefaultEvent(CFailureToPayEvent::typeid)
{
	_phFailureToPayEvent = &childNative;
	_ppFailureToPayEvent = &static_cast<boost::shared_ptr<QuantLib::FailureToPayEvent>>(childNative.currentLink());
    _ppDefaultEvent = new boost::shared_ptr<QuantLib::DefaultEvent> (boost::dynamic_pointer_cast<QuantLib::DefaultEvent> (*_ppFailureToPayEvent));
}
#endif
#ifdef STRUCT
Cephei::QL::Experimental::Credit::CFailureToPayEvent::CFailureToPayEvent (QuantLib::FailureToPayEvent childNative)  : CDefaultEvent(CFailureToPayEvent::typeid)
{
#ifdef HANDLE
	_phFailureToPayEvent = NULL;
#endif
	_ppFailureToPayEvent = new boost::shared_ptr<QuantLib::FailureToPayEvent> (new QuantLib::FailureToPayEvent (childNative));
    _ppDefaultEvent = new boost::shared_ptr<QuantLib::DefaultEvent> (boost::dynamic_pointer_cast<QuantLib::DefaultEvent> (*_ppFailureToPayEvent));
}
#endif

Cephei::QL::Experimental::Credit::CFailureToPayEvent::~CFailureToPayEvent ()
{
    if (_ppFailureToPayEvent != NULL)
    {
	    delete _ppFailureToPayEvent;
        _ppFailureToPayEvent = NULL;
    }
}
Cephei::QL::Experimental::Credit::CFailureToPayEvent::!CFailureToPayEvent ()
{
    if (_ppFailureToPayEvent != NULL)
    {
	    delete _ppFailureToPayEvent;
    }
}
QuantLib::FailureToPayEvent& Cephei::QL::Experimental::Credit::CFailureToPayEvent::GetReference ()
{
    if (_ppFailureToPayEvent == NULL) throw gcnew NativeNullException ();
	return **_ppFailureToPayEvent;
}
boost::shared_ptr<QuantLib::FailureToPayEvent>& Cephei::QL::Experimental::Credit::CFailureToPayEvent::GetShared ()
{
    if (_ppFailureToPayEvent == NULL) throw gcnew NativeNullException ();
	return *_ppFailureToPayEvent;
}
QuantLib::FailureToPayEvent* Cephei::QL::Experimental::Credit::CFailureToPayEvent::GetPointer ()
{
    if (_ppFailureToPayEvent == NULL) throw gcnew NativeNullException ();
	return &**_ppFailureToPayEvent;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::FailureToPayEvent>& Cephei::QL::Experimental::Credit::CFailureToPayEvent::GetHandle ()
{
	if (_phFailureToPayEvent == NULL)
	{
		_phFailureToPayEvent = new Handle<QuantLib::FailureToPayEvent> (*_ppFailureToPayEvent);
	}
	return *_phFailureToPayEvent;
}
#endif
bool Cephei::QL::Experimental::Credit::CFailureToPayEvent::HasNative () 
{
	return (_ppFailureToPayEvent != NULL);
}

Double Cephei::QL::Experimental::Credit::CFailureToPayEvent::AmountDefaulted::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppFailureToPayEvent)->amountDefaulted ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Boolean Cephei::QL::Experimental::Credit::CFailureToPayEvent::MatchesEventType (Cephei::QL::Experimental::Credit::IDefaultType^ contractEvType)
{
    CDefaultType^ _CcontractEvType;
    try
    {
        _CcontractEvType = safe_cast<CDefaultType^> (contractEvType);
        _CcontractEvType->Lock();
        boost::shared_ptr<QuantLib::DefaultType>& _contractEvType = static_cast<boost::shared_ptr<QuantLib::DefaultType>&> (_CcontractEvType->GetShared ()); 
    	bool _rv = (bool)(*_ppFailureToPayEvent)->matchesEventType ( _contractEvType );   
        Boolean _nrv = (Boolean)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_CcontractEvType != nullptr) _CcontractEvType->Unlock();
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Experimental::Credit::IFailureToPayEvent^ Cephei::QL::Experimental::Credit::CFailureToPayEvent_Factory::Create (DateTime creditEventDate, Cephei::QL::ICurrency^ curr, QL::Experimental::Credit::SeniorityEnum bondsSen, Double defaultedAmount, DateTime settleDate, Double recoveryRates)
{
    return gcnew CFailureToPayEvent ( creditEventDate,  curr,  bondsSen,  defaultedAmount,  settleDate,  recoveryRates);
}
